package com.tulele.vtool.handler;

import com.google.common.collect.Maps;
import com.tulele.vtool.model.BPosterDict;
import com.tulele.vtool.model.respEntity.RespData;
import com.tulele.vtool.model.respEntity.ReturnCode;
import com.tulele.vtool.service.BPosterDictService;
import io.micrometer.core.instrument.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 海报分类表(BPosterDict)表控制层
 *
 * @author makejava
 * @since 2020-11-27
 */
@Slf4j
@RestController
@RequestMapping("bPosterDict")
public class BPosterDictHandler {
    /**
     * 服务对象
     */
    @Autowired
    private BPosterDictService bPosterDictService;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @PostMapping(value = "/queryById", produces = "application/json;charset=UTF-8")
    public RespData queryById(@RequestHeader("userId") String userId, @Param("id") Integer id) {
        RespData respData = new RespData();

        if (null == id || StringUtils.isBlank(userId)) {
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("参数异常");
            return respData;
        }

        Map<String, Object> resultMap = Maps.newHashMapWithExpectedSize(1);
        resultMap.put("bBusinessDict", this.bPosterDictService.queryById(id));

        respData.setData(resultMap);
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("查询成功");
        return respData;
    }

    /**
     * 通过 公众号标识、分类编号 查询多条数据
     * 127.0.0.1:8070/bPosterDict/queryByNumber?number=1
     *
     * @param number 分类编号
     * @return 对象列表
     */
    @GetMapping(value = "/queryByNumber", produces = "application/json;charset=UTF-8")
    public RespData queryByNumber(@RequestHeader("userId") String userId, @RequestHeader("identifying") String springProfilesActive, @Param("number") Integer number) {
        RespData respData = new RespData();

        if (StringUtils.isBlank(userId) || StringUtils.isBlank(springProfilesActive) || number == null) {
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("参数异常");
            return respData;
        }

        Map<String, Object> resultMap = Maps.newHashMapWithExpectedSize(1);
        List<BPosterDict> bPosterDict = this.bPosterDictService.queryByNumber(springProfilesActive, number);

        if (CollectionUtils.isNotEmpty(bPosterDict)) {
            resultMap.put("bPosterDict", bPosterDict);
            respData.setData(resultMap);
            respData.setCode(ReturnCode.NORMAL_SUCC);
            respData.setMsg("查询成功");
            return respData;
        }

        respData.setCode(ReturnCode.CLIENT_ERROR);
        respData.setMsg("暂无更多");
        return respData;
    }

    /**
     * 通过 公众号标识、分类编号 查询多条数据
     * 127.0.0.1:8070/bPosterDict/selectByNumber/1
     *
     * @param number 分类编号
     * @return 对象列表
     */
    @GetMapping(value = "/selectByNumber/{number}", produces = "application/json;charset=UTF-8")
    public RespData selectByNumber(@RequestHeader("userId") String userId, @RequestHeader("identifying") String springProfilesActive, @PathVariable("number") Integer number) {

        RespData respData = new RespData();

        if (StringUtils.isBlank(userId) || StringUtils.isBlank(springProfilesActive) || number == null) {
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("参数异常");
            return respData;
        }

        Map<String, Object> resultMap = Maps.newHashMapWithExpectedSize(1);
        List<BPosterDict> bPosterDict = this.bPosterDictService.queryByNumber(springProfilesActive, number);

        if (CollectionUtils.isNotEmpty(bPosterDict)) {
            resultMap.put("bPosterDict", bPosterDict);
            respData.setData(resultMap);
            respData.setCode(ReturnCode.NORMAL_SUCC);
            respData.setMsg("查询成功");
            return respData;
        }

        respData.setCode(ReturnCode.CLIENT_ERROR);
        respData.setMsg("暂无更多");
        return respData;
    }
}